#include <iostream>

using namespace std;

const int N = 10000000;
int dict[N];

int find(int n) {
    if (dict[n]) {
        return dict[n];
    }
    
    //cout << n << endl;

    int next = 0;
    int nn = n;
    while (nn) {
        int d = nn % 10;
        next += d * d;
        nn /= 10;
    }
    return dict[n] = find(next);
}

int main()
{
    dict[1] = 1;
    dict[89] = 89;
    int ans = 0;
    for (int i = 1; i < N; i++) {
        if (find(i) == 89)
            ans += 1;
    }

    cout << ans << endl;
    
    return 0;
}
